package com.sparrow.manage.auth.controller;

import com.sparrow.config.EnvironmentVariables;
import com.sparrow.manage.common.entity.result.Result;
import com.sparrow.manage.company.Company;
import com.sparrow.manage.company.CompanyService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(EnvironmentVariables.MANAGE_ACCESS_PREFIX)
public class LoginController {

    private final static Logger logger = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private CompanyService companyService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(Model model){
        if(SecurityUtils.getSubject().isAuthenticated()){
            return "redirect:index";
        }
        String companyName = companyService.companyName();
        Company company = companyService.queryOne();
        model.addAttribute("companyName", company.getName());
        model.addAttribute("icpNumber", company.getIcpNumber());
        return EnvironmentVariables.MANAGE_TEMPLATE_STORE_PATH + "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public Result login(String username, String password){
        Result result = new Result();

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, DigestUtils.md5DigestAsHex(password.getBytes()));

        try{
            subject.login(token);
            logger.info("登录成功");
            result.setSuccess(true);
        }catch (UnknownAccountException e) {
            logger.error("账号不存在：{}", e);
            result.setMsg("账号不存在");
            return result;
        } catch (DisabledAccountException e) {
            logger.error("账号未启用：{}", e);
            result.setMsg("账号未启用");
            return result;
        } catch (IncorrectCredentialsException e) {
            logger.error("密码错误：{}", e);
            result.setMsg("密码错误");
            return result;
        } catch (RuntimeException e) {
            logger.error("未知错误,请联系管理员：{}", e);
            result.setMsg("未知错误,请联系管理员");
            e.printStackTrace();
            return result;
        }

        return result;
    }

    @RequestMapping("logout")
    @ResponseBody
    public Result logout(){
        Result result = new Result();

        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        result.setSuccess(true);
        return result;
    }



}
